package com.grapro.service.Impl;

import com.grapro.dao.Impl.UserDaoImpl;
import com.grapro.dao.UserDao;
import com.grapro.service.UserService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public boolean loginCheck(int id, String password) {
       boolean flag = false;
        try {
            Map<String, Object> map = userDao.loginCheck(id);
            flag = map.get("password").equals(password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Map<String, Object> getUserInfo(int id) {
        Map<String, Object> map = null;
        try {
            map = userDao.getUserInfo(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;

    }

    @Override
    public boolean updatePassword(int id, String newPassword1, String oldPassword) {
        boolean flag = false;
        try {

            flag = userDao.updatePassword(id, newPassword1, oldPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean addEmployee(Map<String, String[]> parameterMap) {
        boolean flag = false;
        String username = parameterMap.get("name")[0];

        try {
            Map<String, Object> userId = userDao.getUserId(username);
            if (userId == null) {
                flag = userDao.addUser(parameterMap);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean clockIn(String time, String username) {
        String[] arr = time.split(" ");

        boolean flag = false;
        try {
            int userId = (int) userDao.getUserId(username).get("id");
            flag = userDao.clockInInsert(arr, userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
    @Override
    public boolean clockOut(String time, String username) {
        String[] arr = time.split(" ");
        boolean flag = false;

        try {
            int userId = (int) userDao.getUserId(username).get("id");
            Map<String, Object> map = userDao.getTimeId(arr[0], userId);
            if (map == null) {
                flag = userDao.clockOUtInsert(arr, userId);
            } else {
                int id = (int) map.get("timeId");
                flag = userDao.clockOUtInsertByIn(arr, id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
    @Override
    public boolean isHaveChecked(String time, String username) {
        String[] arr = time.split(" ");
        boolean flag = false;
        try {
            Map<String, Object> map1 = userDao.getUserId(username);
            int id = (int) map1.get("id");
            Map<String, Object> map = userDao.getTimeId(arr[0], id);
            flag = (map == null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  flag;
    }

    @Override
    public boolean insertLO(Map<String, String[]> parameterMap, String username) {
        boolean flag = false;
        try {
            Map<String, Object> map  = userDao.getUserId(username);
            if (map != null) {
                int userId = (int) map.get("id");
                flag = userDao.insertLO(parameterMap, userId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<Map<String, Object>> LeaveAndOvertime(int leaveOrOvertimeStatus) {
        List<Map<String, Object>> list = null;
        try {
            list = userDao.LeaveAndOvertime(leaveOrOvertimeStatus);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


}
